﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AlarmManagePlatform.Forms
{
    public partial class CollectTerminalMng : RelationsLMP.Control.Form.MetroDemoForms
    {
        public CollectTerminalMng()
        {
            InitializeComponent();

            InitControls();

            btnSearch_Click(null, null);
        }

        private void InitControls()
        {
            Static.Methods.CreateDGVColumns(ref dgvCollectTerminal, Model.SystemEnum.DGVType.CollectTerminal);
            dgvCollectTerminal.CellClick += DgvCollectTerminal_CellClick;

            treeName.Dock = DockStyle.Fill;
            treeName.Nodes.Clear();
            treeName.DragDropEnabled = false;
            treeName.NodeClick += TreeName_NodeClick;
        }

        private void DgvCollectTerminal_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (sender != null && e.RowIndex >= 0)
            {
                if (dgvCollectTerminal.Rows[e.RowIndex].Cells["Selected"].Value != null && (bool)dgvCollectTerminal.Rows[e.RowIndex].Cells["Selected"].Value)
                    dgvCollectTerminal.Rows[e.RowIndex].Cells["Selected"].Value = false;
                else
                    dgvCollectTerminal.Rows[e.RowIndex].Cells["Selected"].Value = true;
            }
        }

        private void TreeName_NodeClick(object sender, DevComponents.AdvTree.TreeNodeMouseEventArgs e)
        {
            if (e.Node == null || e.Node.Level != 0) return;

            string _Name = e.Node.Text;

            Dictionary<string, string> _LogisticList = GetLogisticList();

            List<Model.DB.tb_DataCollectTerminalInfo> _List = new DataAccess.tb_DataCollectTerminalInfo().GetCollectTerminalInfoList(_Name, _LogisticList);

            dgvCollectTerminal.DataSource = _List;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            List<string> _ListName;
            DevComponents.AdvTree.Node _NodeInfo;

            try
            {

                Dictionary<string, string> _LogisticList = GetLogisticList();

                _ListName = new DataAccess.tb_DataCollectTerminalInfo().GetNameList(_LogisticList);

                _NodeInfo = new DevComponents.AdvTree.Node();

                treeName.Nodes.Clear();

                for (int i = 0; i < _ListName.Count; i++)
                {
                    _NodeInfo = new DevComponents.AdvTree.Node();

                    _NodeInfo.Text = _ListName[i];

                    treeName.Nodes.Add(_NodeInfo);
                }

                if (treeName.Nodes.Count > 0)
                {
                    treeName.SelectedNode = treeName.Nodes[0];
                    TreeName_NodeClick(null, new DevComponents.AdvTree.TreeNodeMouseEventArgs(treeName.Nodes[0], MouseButtons.Left, 0, 0, 0, 0));
                }
                else
                {
                    dgvCollectTerminal.DataSource = null;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "查询终端设备失败，错误信息：" + ex.Message, "异常信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            List<string> _IndexList = new List<string>();
            bool _Select;
            for (int i = 0; i < dgvCollectTerminal.Rows.Count; i++)
            {
                if (dgvCollectTerminal.Rows[i].Cells["Selected"].Value == null) continue;
                _Select = (bool)dgvCollectTerminal.Rows[i].Cells["Selected"].Value;
                if (_Select)
                    _IndexList.Add(dgvCollectTerminal.Rows[i].Cells["Id"].Value.ToString());
            }

            if (_IndexList.Count <= 0)
            {
                MessageBox.Show(this, "请选择【IMEI】信息！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (MessageBox.Show("是否确认删除？", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
            {
                try
                {
                    new DataAccess.tb_DataCollectTerminalInfo().DeleteCollectTerminal(_IndexList);

                    btnSearch_Click(null, null);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, "删除终端设备失败，错误信息：" + ex.Message, "异常信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Forms.CollectTerminalForm _CollectTerminalForm = new Forms.CollectTerminalForm();
            if (_CollectTerminalForm.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    new DataAccess.tb_DataCollectTerminalInfo().SaveCollectTerminalInfo(_CollectTerminalForm.DataCollectTerminalInfo);

                    btnSearch_Click(null, null);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, "保存终端设备失败，错误信息：" + ex.Message, "异常信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private Dictionary<string, string> GetLogisticList()
        {
            Dictionary<string, string> _Result = new Dictionary<string, string>();

            _Result.Add("Name", checkName.Checked ? txtName.Text : string.Empty);

            _Result.Add("IMEI", checkIMEI.Checked ? txtIMEI.Text : string.Empty);

            return _Result;
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            if (dgvCollectTerminal.SelectedRows.Count <= 0)
            {
                MessageBox.Show(this, "请选择采集终端信息！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            Model.DB.tb_DataCollectTerminalInfo _Temp = new DataAccess.tb_DataCollectTerminalInfo().GetInfoForId(dgvCollectTerminal.SelectedRows[0].Cells["Id"].Value.ToString());

            if (_Temp == null)
            {
                MessageBox.Show(this, "采集终端下无相关信息！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            Forms.CollectTerminalForm _CollectTerminalForm = new Forms.CollectTerminalForm();
            _CollectTerminalForm.ModifyInfo(_Temp);

            if (_CollectTerminalForm.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    _CollectTerminalForm.DataCollectTerminalInfo.Id = _Temp.Id;
                    new DataAccess.tb_DataCollectTerminalInfo().UpdateDataCollectTerminal(_CollectTerminalForm.DataCollectTerminalInfo);

                    btnSearch_Click(null, null);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("修改采集终端失败，错误信息：" + ex.ToString());
                }
            }
        }
    }
}
